<?php

/*
 * 阿里云 短信验证码
 */
if (! function_exists('sms_send')) {
    function sms_send(Array $param){
        /*$ali_sms = new \Overtrue\EasySms\EasySms(config('sms.config'));
        $result = $ali_sms->send($param['mobile'], [
            'content'  => '',
            'template' => $param['templet_id'],
            'data' => [
                'code' => $param['content']['code']
            ],
        ]);
        if(strtolower($result['aliyun']['status']) != 'success'){
            return false;
        }*/
        // 短信验证码记录
        $sms = new \App\Models\Sms();
        $sms->mobile = $param['mobile'];
        $sms->templet = $param['templet_id'];
        $sms->code = $param['content']['code'];
        // $sms->result = json_encode($result);
        $sms->result = json_encode([]);
        $sms->ip = $param['ip'];
        $sms->save();
        return true;
    }
}
if (!function_exists('formatTime')) {
    function formatTime($date) {
        $str = '';
        $timer = strtotime($date);
        $diff = $_SERVER['REQUEST_TIME'] - $timer;
        $day = floor($diff / 86400);
        $free = $diff % 86400;
        if($day > 0) {
            return $day."天前";
        }else{
            if($free>0){
                $hour = floor($free / 3600);
                $free = $free % 3600;
                if($hour>0){
                    return $hour."小时前";
                }else{
                    if($free>0){
                        $min = floor($free / 60);
                        $free = $free % 60;
                        if($min>0){
                            return $min."分钟前";
                        }else{
                            if($free>0){
                                return $free."秒前";
                            }else{
                                return '刚刚';
                            }
                        }
                    }else{
                        return '刚刚';
                    }
                }
            }else{
                return '刚刚';
            }
        }
    }
}
/**
 * 两点之间距离
 * @param $lat1
 * @param $lon1
 * @param $lat2
 * @param $lon2
 * @param float $radius  星球半径 KM
 * @return float
 */
if (! function_exists('get_distance')) {
    function get_distance($lat1, $lon1, $lat2, $lon2, $radius = 6378.137){
        $rad = floatval(M_PI / 180.0);

        $lat1 = floatval($lat1) * $rad;
        $lon1 = floatval($lon1) * $rad;
        $lat2 = floatval($lat2) * $rad;
        $lon2 = floatval($lon2) * $rad;

        $theta = $lon2 - $lon1;

        $dist = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta));

        if ($dist < 0 ) {
            $dist += M_PI;
        }
        return $dist = round($dist * $radius, 2);
    }
}

